package com.xsmart.oa.services;

import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.oa.dao.ShopManageDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lxh on 2018/1/5.
 */
@Service
public class ShopManageService {
    @Autowired
    ShopManageDao shopManageDao;

    /**
     * 查询所有的商家
     * @param requestBean
     * @return
     */
    public ResponseBean queryAllSeller(RequestBean requestBean) {
        ResponseBean responseBean=requestBean.getResponseBean();
        int currentPage=requestBean.getRequestParamInteger("currentPage",1);
        int pageSize=requestBean.getRequestParamInteger("pageSize",10);
        Integer id=requestBean.getRequestParamInteger("id");
        String shopName=requestBean.getRequestParamString("shopName");
        Page<Map<String,Object>>page=new Page<>(currentPage,pageSize);
        page.setRecords(shopManageDao.queryAllSeller(page,id,shopName));
        responseBean.addResultPageItem("allSeller",page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 修改商家信息
     * @param requestBean
     * @return
     */
    public ResponseBean updateSeller(RequestBean requestBean) {
        ResponseBean responseBean=requestBean.getResponseBean();
        String[][] files = new String[][]{{"id", ""},
                {"shop_name", ""},
                {"shop_title", ""},
                {"shop_address", ""},
                {"seller_email", ""},
                {"mobile", ""},
                {"notice", ""}};
        Map<String, Object> ShopInfo= new HashMap<>();
        for (String[] file : files) {
            Object val = requestBean.getRequestParam(file[0]);
            if (val == null) {
                //传递过来的值如果为空，就设置默认值
                ShopInfo.put(file[0], file[1]);
            } else {
                //不为空，则设置成传递过来的值
                ShopInfo.put(file[0], requestBean.getRequestParam(file[0]));
            }
        }
        int flag=shopManageDao.updateSeller(ShopInfo);
        if(flag>0){
            responseBean.setResult(ResultConstant.SUCCESS);
        }
        return responseBean;
    }

    /**
     * 查询店铺所拥有的货物
     * @param requestBean
     * @return
     */
    public ResponseBean queryShopGoodById(RequestBean requestBean) {
        ResponseBean responseBean=requestBean.getResponseBean();
        int currentPage=requestBean.getRequestParamInteger("currentPage",1);
        int pageSize=requestBean.getRequestParamInteger("pageSize",10);
        int id=requestBean.getRequestParamInteger("id");
        Page<Map<String,Object>>page=new Page<>(currentPage,pageSize);
//与商品列表显示信息相同
        Map<String, Object> goodsMap=new HashMap<>();
        goodsMap.put("goods_id","");
        goodsMap.put("goods_name","");
        goodsMap.put("theShopId",id);
        page.setRecords(shopManageDao.queryShopGoodByShopId(page,goodsMap));
//        page.setRecords(shopManageDao.queryShopGoodById(page,id));
        responseBean.addResultPageItem("ShopGoods",page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }
    /**
     * 查询所有货物
     * @param requestBean
     * @return
     */
    public ResponseBean queryAllGoods(RequestBean requestBean) {
        ResponseBean responseBean=requestBean.getResponseBean();
        int currentPage=requestBean.getRequestParamInteger("currentPage",1);
        int pageSize=requestBean.getRequestParamInteger("pageSize",10);
        Integer id=requestBean.getRequestParamInteger("id");
        Page<Map<String,Object>>page=new Page<>(currentPage,pageSize);
//        page.setRecords(shopManageDao.queryAllGoods(page,id));
//与商品列表显示信息相同
        Map<String, Object> goodsMap=new HashMap<>();
        goodsMap.put("goods_id","");
        goodsMap.put("goods_name","");
        goodsMap.put("theShopId",id);
        page.setRecords(shopManageDao.queryGoods(page,goodsMap));

        responseBean.addResultPageItem("allGoods",page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 插入对应商家的货物
     * @param requestBean
     * @return
     */

    public ResponseBean insertShopGoods(RequestBean requestBean) {
        String[][] files = new String[][]{{"goods_id", ""},
                {"id", ""},
                {"shop_price", ""},
                {"goods_name", ""},
                {"goods_sn", ""},
                {"supply_price", ""}};
        Map<String, Object> ShopInfo= new HashMap<>();
        for (String[] file : files) {
            Object val = requestBean.getRequestParam(file[0]);
            if (val == null) {
                //传递过来的值如果为空，就设置默认值
                ShopInfo.put(file[0], file[1]);
            } else {
                //不为空，则设置成传递过来的值
                ShopInfo.put(file[0], requestBean.getRequestParam(file[0]));
            }
        }
        ResponseBean responseBean=requestBean.getResponseBean();
        int i=shopManageDao.insertShopGoods(ShopInfo);
        if(i>0){
            responseBean.setResult(ResultConstant.SUCCESS);
        }else{
            responseBean.setResult(ResultConstant.ERROR);
        }
        return responseBean;
    }
    /**
     * 删除对应商家的货物
     * @param requestBean
     * @return
     */
    public ResponseBean deleteShopGoods(RequestBean requestBean) {
        ResponseBean responseBean=requestBean.getResponseBean();
        Integer id=requestBean.getRequestParamInteger("id");
        Integer goodsId=requestBean.getRequestParamInteger("goodsId");
        int i=shopManageDao.deleteShopGoods(id,goodsId);
        if(i>0){
            responseBean.setResult(ResultConstant.SUCCESS);
        }else{
            responseBean.setResult(ResultConstant.ERROR);
        }
        return responseBean;
    }

}
